package com.atguigu.gulimall.seckill.controller;

import com.atguigu.gulimall.common.utils.R;
import com.atguigu.gulimall.seckill.service.SecKillService;
import com.atguigu.gulimall.seckill.to.SecKillSkuRedisTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author: xuesm
 * @date: 2025/3/15 23:14
 */
@RestController
public class SecKillController {
    @Autowired
    SecKillService secKillService;

    /**
     * 当前时间可以参与的秒杀商品信息
     * @return
     */
    @GetMapping("/currentSecKillSkus")
    public R getCurrentSecKillSkus(){
        List<SecKillSkuRedisTo> tos = secKillService.getCurrentSecKillSkus();
        return R.ok().setData(tos);
    }

    /**
     * 当前sku是否参与秒杀优惠
     */
    @GetMapping("/sku/seckill/{skuId}")
    public R getSkuSecKillInfo(@PathVariable("skuId") Long skuId){
        SecKillSkuRedisTo to = secKillService.getSkuSecKillInfo(skuId);
        return R.ok().setData(to);
    }

    @GetMapping("/kill")
    public R kill(@RequestParam("killId") String killId,
                  @RequestParam("key") String key,
                  @RequestParam("num") Integer num){

        String orderSn = secKillService.kill(killId, key, num);
        return R.ok().setData(orderSn);
    }
}
